import lodash from 'lodash';
import { catIndex } from '../services/index-cat-controller';

export default {
  namespace: 'GlobalEnumModel',

  state: {
    // 所有的系统名称
    sysNameList: [
      { index: '*', sysName: '所有系统' },
    ],
    currentSysName: "*",
  },

  effects: {
    *allSysName(_, { call, put }) {
      // 请求数据
      const indexList = yield call(catIndex);
      if (!indexList) return;
      // 处理系统
      const sysNameList = [{ index: '*', sysName: '所有系统' }];
      lodash.forEach(indexList, ({ index }) => {
        let tmp = null;
        if (lodash.startsWith(index, "per-sqls-")) {
          tmp = lodash.replace(index, "per-sqls-", "");
        } else if (lodash.startsWith(index, "per-summary-")) {
          tmp = lodash.replace(index, "per-summary-", "");
        } else if (lodash.startsWith(index, "per-uris-")) {
          tmp = lodash.replace(index, "per-uris-", "");
        } else if (lodash.startsWith(index, "per-jvms-")) {
          tmp = lodash.replace(index, "per-jvms-", "");
        }
        if (tmp !== null && lodash.findIndex(sysNameList, { index: tmp }) < 0) {
          sysNameList.push({ index: tmp, sysName: tmp });
        }
      });
      yield put({ type: 'save', payload: { sysNameList } });
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
